package com.lei.day05lx.presenter;

import android.util.Log;

import com.google.gson.Gson;
import com.lei.day05lx.bean.DxcBean;
import com.lei.day05lx.bean.LoginBean;
import com.lei.day05lx.bean.RegisterBean;
import com.lei.day05lx.contract.LoginContract;
import com.lei.tong.log_utlis.LogUtils;
import com.lei.tong.mvp.presenter.BasePresenter;
import com.lei.tong.retrofit.BaseObservable;
import com.lei.tong.retrofit.BaseObserver;

import java.util.HashMap;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> implements LoginContract.Presenter {

    public LoginPresenter(LoginContract.Model mmodel, LoginContract.View mview) {
        super(mmodel, mview);
    }

    @Override
    public void login() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",mview.getuser());
        map.put("password",mview.getpwd());
        new BaseObservable().doit(mmodel.login(map),new BaseObserver<LoginBean>(){
            @Override
            public void onNext(LoginBean loginBean) {
                super.onNext(loginBean);
                if (loginBean.getErrorCode()==0){
                    LogUtils.e("登陆成功");
                }else {
                    LogUtils.e("登录失败");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtils.e(""+e);
            }
        });
    }

    @Override
    public void register() {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",mview.getuser());
        map.put("password",mview.getpwd());
        map.put("repassword",mview.getpwd());
        new BaseObservable().doit(mmodel.register(map),new BaseObserver<RegisterBean>(){
            @Override
            public void onNext(RegisterBean registerBean) {
                super.onNext(registerBean);
                if (registerBean.getErrorCode()==0){
                    LogUtils.e("注册成功");
                }else {
                    LogUtils.e("注册失败");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtils.e(""+e);
            }
        });
    }

    @Override
    public void initdata() {
        HashMap<String, String> map = new HashMap<>();
        map.put("stage_id","1");
        map.put("limit","10");
        map.put("page","10");
        new BaseObservable().doit(mmodel.dxc(map),new BaseObserver<DxcBean>(){
            @Override
            public void onNext(DxcBean dxcBean) {
                super.onNext(dxcBean);
                if (dxcBean.getData()!=null){
                    LogUtils.e("请求成功"+dxcBean.toString());
                    mview.Dxc(dxcBean.toString());
                }else {
                    LogUtils.e("请求失败");
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                LogUtils.e(""+e);
            }
        });
    }

    @Override
    public void destroy() {

    }
}
